package com.jhx.mall.app.servlet;

import com.jhx.mall.app.service.DtsCartService;
import com.jhx.mall.app.service.impl.DtsCartServiceImpl;
import com.jhx.mall.entity.DtsCart;
import com.jhx.mall.entity.DtsCategory;
import com.jhx.mall.util.BaseServlet;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "DtsCart", value = "/api/cart/DtsCart")
public class DtsCartSerlvet extends BaseServlet {
    private DtsCartService dtsCartService;

    public void init() throws ServletException {
        dtsCartService = new DtsCartServiceImpl();
    }
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String opr=request.getParameter("opr");
        if(opr.equals("list")){
            Long userId= Long.parseLong(request.getParameter("userId"));
            List<DtsCart> list=dtsCartService.selectCartByUserId(userId);
            if(list!=null){
                ok(request,response,list);
            }else {
                error(request,response,list);
            }
        } else if (opr.equals("delete")) {
            Long id= Long.parseLong(request.getParameter("id"));
            int rs=dtsCartService.deleteCartByUserId(id);
            if(rs!=0){
                ok(request,response,rs);
            }else {
                error(request,response,rs);
            }
        } else if (opr.equals("update")) {
            Long id= Long.parseLong(request.getParameter("id"));
            Long number= Long.parseLong(request.getParameter("number"));
            int rs=dtsCartService.updateCartByUserId(id,number);
            if(rs!=0){
                ok(request,response,rs);
            }else {
                error(request,response,rs);
            }
        }
    }
}